package fmse.uaic.k.configuration_abstraction.data_structures;

import java.util.List;

import fmse.uaic.k.configuration_abstraction.abstract_classes.KAbstractNode;
import fmse.uaic.k.configuration_abstraction.abstract_classes.KAbstractTree;
import fmse.uaic.k.configuration_abstraction.exceptions.NotDeclaredLabelException;
import fmse.uaic.k.configuration_abstraction.visitors.CheckLabelDeclarationVisitor;

public class RuleKTree extends KAbstractTree {

	private RuleKNode ruleRoot;
	private int line;

	public RuleKTree(RuleKNode root, int line_number) {
		super();
		this.root = root;
		this.setRuleRoot(root);
		line = line_number;
	}

	public RuleKTree(RuleKNode root) {
		super();
		this.root = root;
		this.setRuleRoot(root);
		line = -1; // unknown line number
	}

	/**
	 * Check if there are labels in the rule which are not declared in a given
	 * configuration
	 * 
	 * @param ctree
	 * @throws NotDeclaredLabelException
	 */
	public void checkLabelsDeclarations(ConfigurationKTree ctree)
			throws NotDeclaredLabelException {
		CheckLabelDeclarationVisitor declarationVisitor = new CheckLabelDeclarationVisitor(
				ctree);
		dfs(declarationVisitor);
		List<KAbstractNode> missingDeclarations = declarationVisitor
				.getUndefined();
		if (missingDeclarations.size() > 0)
			throw new NotDeclaredLabelException(root, line, missingDeclarations);
	}

	public void setRuleRoot(RuleKNode ruleRoot) {
		this.ruleRoot = ruleRoot;
	}

	public RuleKNode getRuleRoot() {
		return ruleRoot;
	}

}
